package com.guofen.controller.shlj;

import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.SjqWaterProductionDeliverWater;
import com.guofen.pojo.User;
import com.guofen.service.shlj.AbnormalGarbageService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/shlj/abnormolGarbage")
@Controller
@Api(tags = "AbnormalGarbageController", description = "异常垃圾情况登记表")
public class AbnormalGarbageController {

    @Autowired
    private AbnormalGarbageService service;

    @ApiOperation("添加记录")
    @RequestMapping(value = "/addForm",method = RequestMethod.POST)
    @ResponseBody
    public Result addForm(@ApiParam(name="wagon_number",value="车号",required=true) @RequestParam String wagon_number,
                          @ApiParam(name="garbage_source",value="垃圾来源(区域)",required=true) @RequestParam String garbage_source,
                          @ApiParam(name="garbage_type",value="垃圾种类",required=true) @RequestParam String garbage_type,
                          @ApiParam(name="garbage_number",value="数量",required=true) @RequestParam String garbage_number,
                          @ApiParam(name="recording_person",value="记录人",required=true) @RequestParam String recording_person,
                          @ApiParam(name="remarks",value="备注",required=false) @RequestParam String remarks){
        double garbageNumber = Double.parseDouble(garbage_number);
        if (service.addForm(wagon_number, garbage_source, garbage_type, garbageNumber, recording_person, remarks))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(service.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="wagon_number",value="车号",required=true) @RequestParam String wagon_number,
                         @ApiParam(name="garbage_source",value="垃圾来源(区域)",required=true) @RequestParam String garbage_source,
                         @ApiParam(name="garbage_type",value="垃圾种类",required=true) @RequestParam String garbage_type,
                         @ApiParam(name="garbage_number",value="数量",required=true) @RequestParam Double garbage_number,
                         @ApiParam(name="recording_person",value="记录人",required=true) @RequestParam String recording_person,
                         @ApiParam(name="remarks",value="备注",required=false) @RequestParam String remarks) {
        ShljAbnormalGarbage record = new ShljAbnormalGarbage();
        record.setId(id);
        record.setWagonNumber(wagon_number);
        record.setGarbageSource(garbage_source);
        record.setGarbageType(garbage_type);
        record.setGarbageNumber(garbage_number);
        record.setRecordingPerson(recording_person);
        record.setRemarks(remarks);
        return ResultGenerator.genSuccessResult(service.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(service.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(service.getByTime(pageSize, currentPage, startTime, endTime));
    }
    
    @ApiOperation("EXCEL批量添加记录")
    @PostMapping("/addFromExcel")
    @ResponseBody
    public Result doRegisterFromExcel(@RequestParam("file") MultipartFile file){
        String EXCEL_XLS = "xls";
        String EXCEL_XLSX = "xlsx";

        if (file.isEmpty())
            return ResultGenerator.genFailResult("文件为空");
        System.out.println(file.getOriginalFilename());

        List<Boolean> registerResult = new ArrayList<>();
        try {
            //根据路径获取这个操作excel的实例
            Sheet sheet = null;
            Row row = null;
            if (file.getOriginalFilename().endsWith(EXCEL_XLS)) {
                //用于xls文件的读取
                HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
                sheet = wb.getSheetAt(0);
            } else if (file.getOriginalFilename().endsWith(EXCEL_XLSX)) {
                //用于xlsx文件的读取
                XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
                //根据页面index 获取sheet页
                sheet = wb.getSheetAt(0);
            }
            //实体类集合
            List<ShljAbnormalGarbage> shljAbnormalGarbages = new ArrayList<>();

            //XSSFRow row = null;
            //循环sheet页中数据从第三行开始，第一行是导入说明，第二行是标题
            System.out.println(sheet.getPhysicalNumberOfRows());
            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                //获取每一行数据
                row = sheet.getRow(i);
                //如果表格第二列和第三列为空
                if((row.getCell(1)==null&&row.getCell(2)==null&&row.getCell(3)==null&&row.getCell(4)==null
                        &&row.getCell(5)==null&&row.getCell(6)==null&&row.getCell(7)==null)
                        ||row.getCell(2).getNumericCellValue()==0||row.getCell(5).getNumericCellValue()==0){
                    continue;
                }
                ShljAbnormalGarbage shljAbnormalGarbage = new ShljAbnormalGarbage();
                //表中第二列，日期
                if(row.getCell(1)!=null){
                    Cell cell = row.getCell(1);
                    Date d = cell.getDateCellValue();
                    DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
                    String guarantee_date = "";
                    guarantee_date = formater.format(d);
                    shljAbnormalGarbage.setDate(guarantee_date);
                }
                if(row.getCell(2)!=null){
                    Cell cell = row.getCell(2);
                    cell.setCellType(CellType.STRING);
                    shljAbnormalGarbage.setWagonNumber(cell.getStringCellValue());
                }
                if(row.getCell(3)!=null){
                    Cell cell = row.getCell(3);
                    cell.setCellType(CellType.STRING);
                    shljAbnormalGarbage.setGarbageSource(cell.getStringCellValue());
                }
                if(row.getCell(4)!=null){
                    Cell cell = row.getCell(4);
                    cell.setCellType(CellType.STRING);
                    shljAbnormalGarbage.setGarbageType(cell.getStringCellValue());
                }
                if(row.getCell(5)!=null){
                    shljAbnormalGarbage.setGarbageNumber((double)row.getCell(5).getNumericCellValue());
                }
                if(row.getCell(6)!=null){
                    Cell cell = row.getCell(6);
                    cell.setCellType(CellType.STRING);
                    shljAbnormalGarbage.setGarbageUnit(cell.getStringCellValue());
                }
                if(row.getCell(7)!=null){
                    Cell cell = row.getCell(7);
                    cell.setCellType(CellType.STRING);
                    shljAbnormalGarbage.setRecordingPerson(cell.getStringCellValue());
                }
                if(row.getCell(8)!=null){
                    Cell cell = row.getCell(8);
                    cell.setCellType(CellType.STRING);
                    shljAbnormalGarbage.setRemarks(cell.getStringCellValue());
                }
                System.out.println(shljAbnormalGarbage.getWagonNumber());
                shljAbnormalGarbages.add(shljAbnormalGarbage);
            }
            for (ShljAbnormalGarbage u : shljAbnormalGarbages) {
                Boolean test = false;
                registerResult.add(test = service.addFormExcel(
                        u.getDate(),u.getWagonNumber(), u.getGarbageSource(), u.getGarbageType(),
                        u.getGarbageNumber(), u.getGarbageUnit(), u.getRecordingPerson(),u.getRemarks()));
                System.out.println(test);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultGenerator.genSuccessResult(registerResult);
    }

    @ApiOperation("查询记录")
    @RequestMapping(value = "/findForm", method = RequestMethod.GET)
    @ResponseBody
    public Result findForm(){
        if (service.findForm() != null)
            return ResultGenerator.genSuccessResult(service.findForm());
        else
            return ResultGenerator.genFailResult("暂无记录");
    }
}
